//
//  CeoTeBieXVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/18.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "CeoTeBieXVC.h"

#import "DouDetailsCell.h"
#import "TodoHttpManager.h"
#import "CEOTeBieVC.h"

#import "LXAlertView.h"
@interface CeoTeBieXVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    NSString *str;
    NSString *str1;
    NSString *str2;
    NSString *str3;
    NSString *str4;
    NSString *str5;
    NSString *str6;
    NSString *str7;
    NSString *str8;
    NSString *str9;
    NSString *str10;
    NSString *str11;
    NSString *str12;
    NSString *str13;
    NSString *str14;
}
@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableArray *titleArr;
@property(nonatomic,strong)NSMutableArray *oneArr;
@property(nonatomic,strong)NSMutableArray *twoArr;
@property(nonatomic,strong)NSMutableDictionary *zoDic;
@property(nonatomic,strong)UITextField *oneTF;
@property(nonatomic,strong)UITextField *twoTF;
@property(nonatomic,strong)UITextField *threeTF;


@end

@implementation CeoTeBieXVC

-(void)viewWillAppear:(BOOL)animated
{
    //TODO:特别优惠政策详情
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoTebIeYouHX:) name:TDCeoTebIeYouHX object:nil];
    [_manager userCeoTeBieYouHuiXWithID:_ID];
    
    
    //TODO:特别优惠政策审批
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoTeBieyouHShenP:) name:TDCeoTeBieyouHShenP object:nil];
    
    
}
-(void)didTDCeoTebIeYouHX:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        _zoDic = [notifi.object objectForKey:@"data"];
        
        str = [_zoDic objectForKey:@"sq_date"];
        str1 = [_zoDic objectForKey:@"kh_name"];
        str2 = [_zoDic objectForKey:@"kh_phone"];
        str3 = [_zoDic objectForKey:@"fy_name"];
        str4 = [_zoDic objectForKey:@"sale_area"];
        str5 = [_zoDic objectForKey:@"price"];
        str6 = [_zoDic objectForKey:@"price_all"];
        str7 = [_zoDic objectForKey:@"renchou_yh"];
        str8 = [_zoDic objectForKey:@"acyh"];
        str9 = [_zoDic objectForKey:@"xieyi_all"];
        str10 = [_zoDic objectForKey:@"xieyi_dan"];
        str11 = [_zoDic objectForKey:@"remark"];
        str12 = [_zoDic objectForKey:@"special_cash_discount"];
        str13 = [_zoDic objectForKey:@"special_area_discount"];
        str14 = [_zoDic objectForKey:@"special_discount"];
     
        if ([_statueStr isEqualToString:@"1"])
        {
            _oneArr = [[NSMutableArray alloc] initWithObjects:str,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11, nil];
        }
       if ([_statueStr isEqualToString:@"2"]||[_statueStr isEqualToString:@"3"])
        {
            _oneArr = [[NSMutableArray alloc] initWithObjects:str,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14, nil];
        }

        
        [_tabView reloadData];
        
    }
}


//优惠政策审批
-(void)didTDCeoTeBieyouHShenP:(NSNotification *)noti
{
    if (noti.object != nil)
    {
        int status = [[noti.object objectForKey:@"status"] intValue];
        NSString *infoStr = [noti.object objectForKey:@"info"];
        if (status == 1)
        {
            [_manager MakeMyselfAlert:infoStr andIsNeedTitle:NO andShowTime:1.5];
            //CEOTeBieVC *youVC = [[CEOTeBieVC alloc] init];
            [self.navigationController popViewControllerAnimated:NO];
        }
        if (status == 2)
        {
            [_manager MakeMyselfAlert:infoStr andIsNeedTitle:NO andShowTime:1.5];
           // CEOTeBieVC *youVC = [[CEOTeBieVC alloc] init];
            [self.navigationController popViewControllerAnimated:NO];
        }
    }
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoTebIeYouHX object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoTeBieyouHShenP object:nil];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"特别优惠审批";
    self.view.backgroundColor = BgColor;
    
    _zoDic = [NSMutableDictionary dictionary];
    _twoArr = [NSMutableArray array];
    _oneArr = [NSMutableArray array];
    if ([_statueStr isEqualToString:@"1"])
    {
        _titleArr = [NSMutableArray arrayWithObjects:@"申请日期:",@"客户姓名:",@"联系电话:",@"房源位置:",@"销售面积:",@"销售表价:",@"表价总价:",@"认筹优惠:",@"案场优惠:",@"协议总价:",@"协议单价:",@"备注信息:", nil];
    }
    else
    {
        _titleArr = [NSMutableArray arrayWithObjects:@"申请日期:",@"客户姓名:",@"联系电话:",@"房源位置:",@"销售面积:",@"销售表价:",@"表价总价:",@"认筹优惠:",@"案场优惠:",@"协议总价:",@"协议单价:",@"备注信息:",@"现金优惠",@"单价优惠:",@"折扣优惠:", nil];
    }
    
    _manager = [TodoHttpManager sharedManager];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, -40, B_WIDTH, B_HEIGHT) style:UITableViewStyleGrouped];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.backgroundColor = BgColor;
    
    _tabView.rowHeight = 40;
   // _tabView.tableFooterView =
    _tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tabView];

    
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    
    return _titleArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndent = @"CellB";
    
    
    DouDetailsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell ==nil)
    {
        cell = [[DouDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
        
        
    }
    
    if ([cell isKindOfClass:[DouDetailsCell class]])
    {
        if (_oneArr.count !=0)
        {
            
            NSString *str0 = (NSString *)_oneArr[indexPath.row];
            cell.secondLab.text = [NSString stringWithFormat:@"%@",str0];
            
            
        }
        
        
    }
    
    cell.firstLab.text = _titleArr[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
    return cell;
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([_statueStr isEqualToString:@"1"])
    {
        return 400;
    }
    else
    {
        return 1;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    if ([_statueStr isEqualToString:@"1"])
    {
        UIView *oneView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 400)];
        oneView.backgroundColor = BgColor;
        [self.view addSubview:oneView];
        UILabel *titlLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 35)];
        titlLab.backgroundColor = [UIColor orangeColor];
        titlLab.text = @"    特批优惠额度(任选其一)";
        titlLab.textColor = [UIColor whiteColor];
        titlLab.font = [UIFont systemFontOfSize:14];
        [oneView addSubview:titlLab];
        
        UILabel *xianLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 36, B_WIDTH, 40)];
        xianLab.backgroundColor = [UIColor whiteColor];
        xianLab.text = @"    现金优惠 :";
        xianLab.textColor = [UIColor blackColor];
        xianLab.font = [UIFont systemFontOfSize:14];
        [oneView addSubview:xianLab];

        _oneTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 40, 150, 30)];
        _oneTF.textColor = [UIColor blackColor];
        _oneTF.borderStyle = UITextBorderStyleRoundedRect;
        _oneTF.delegate = self;
        [oneView addSubview:_oneTF];
        
        UILabel *hOneLab = [[UILabel alloc] initWithFrame:CGRectMake(255, 36,15, 40)];
        hOneLab.text = @"元";
        hOneLab.textColor = [UIColor blackColor];
        hOneLab.font = [UIFont systemFontOfSize:14];
        [oneView addSubview:hOneLab];
        
        
        UILabel *danLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 78, B_WIDTH, 40)];
        danLab.backgroundColor = [UIColor whiteColor];
        danLab.text = @"    单价优惠 :";
        danLab.textColor = [UIColor blackColor];
        danLab.font = [UIFont systemFontOfSize:14];
        [oneView addSubview:danLab];

        _twoTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 83, 150, 30)];
        _twoTF.textColor = [UIColor blackColor];
        _twoTF.borderStyle = UITextBorderStyleRoundedRect;
        _twoTF.delegate = self;
        [oneView addSubview:_twoTF];
        
        UILabel *hTwoLab = [[UILabel alloc] initWithFrame:CGRectMake(255, 76,40, 40)];
        hTwoLab.text = @"元/㎡";
        hTwoLab.textColor = [UIColor blackColor];
        hTwoLab.font = [UIFont systemFontOfSize:14];
        [oneView addSubview:hTwoLab];
        
        
        UILabel *zheLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, B_WIDTH, 40)];
        zheLab.backgroundColor = [UIColor whiteColor];
        zheLab.text = @"    折扣优惠 :";
        zheLab.textColor = [UIColor blackColor];
        zheLab.font = [UIFont systemFontOfSize:14];
        [oneView addSubview:zheLab];
        
        _threeTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 125, 150, 30)];
        _threeTF.textColor = [UIColor blackColor];
        _threeTF.borderStyle = UITextBorderStyleRoundedRect;
        _threeTF.delegate = self;
        [oneView addSubview:_threeTF];
        
        UILabel *hThrLab = [[UILabel alloc] initWithFrame:CGRectMake(255, 120,15, 40)];
        hThrLab.text = @"%";
        hThrLab.textColor = [UIColor blackColor];
        hThrLab.font = [UIFont systemFontOfSize:14];
        [oneView addSubview:hThrLab];
        
        
        
        UIButton *arrgeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        arrgeBtn.frame = CGRectMake(15, 220, B_WIDTH-15*2, 40);
        arrgeBtn.backgroundColor = [UIColor orangeColor];
        arrgeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        arrgeBtn.layer.cornerRadius = 5.0f;
        [arrgeBtn setTitle:@"按批执行" forState:0];
        arrgeBtn.tag = 88;
        [arrgeBtn addTarget:self action:@selector(agreeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [arrgeBtn setTitleColor:[UIColor whiteColor] forState:0];
        [oneView addSubview:arrgeBtn];
        
         UIButton *unArrgeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        unArrgeBtn.frame = CGRectMake(15, 280, B_WIDTH-15*2, 40);
        unArrgeBtn.backgroundColor = [UIColor orangeColor];
        unArrgeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        unArrgeBtn.layer.cornerRadius = 5.0f;
        [unArrgeBtn setTitle:@"驳回申请" forState:0];
        [unArrgeBtn setTitleColor:[UIColor whiteColor] forState:0];
        unArrgeBtn.tag = 89;
        [unArrgeBtn addTarget:self action:@selector(agreeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [oneView addSubview:unArrgeBtn];
        
        
        return oneView;
 
    }
    else
    {
        return nil;
    }
    
}

-(void)agreeBtnClick:(UIButton *)btn
{
    
    
    NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
    if (btn.tag == 88)
    {
    
        LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"按批执行?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
            
            if (clickIndex == 1)
            {
                
             
                [_manager userCeoTeBieYouHShenPiWithYID:_ID withUID:IDA withType:@"2" withDJYH:_oneTF.text withZJYH:_twoTF.text withBL:_threeTF.text];
            }
       
        }];

        [agreeView showLXAlertView];
    }
    else
    {
        
        
        LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"驳回申请?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
            
            if (clickIndex == 1)
            {
                
                
                
                [_manager userCeoTeBieYouHShenPiWithYID:_ID withUID:IDA withType:@"3" withDJYH:_oneTF.text withZJYH:_twoTF.text withBL:_threeTF.text];
            }
            
        }];
        
        [agreeView showLXAlertView];
        
        
       
    }
   
    
    
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_oneTF resignFirstResponder];
      [_twoTF resignFirstResponder];
      [_threeTF resignFirstResponder];
    
    
    
    
    
    
    
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
